package com.service.business.notify.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.business.member.service.FeedbackService;
import com.service.business.news.service.NewsService;
import com.service.business.notify.service.NotifyMessageService;
import com.service.dao.enums.NotifyMessageEnums;
import com.service.dao.mapper.NotifyMessageMapper;
import com.service.dao.model.Feedback;
import com.service.dao.model.News;
import com.service.dao.model.NotifyMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

@SuppressWarnings("ALL")
@Service
@Slf4j
public class NotifyMessageServiceImpl extends ServiceImpl<NotifyMessageMapper, NotifyMessage> implements NotifyMessageService {

    @Autowired
    private NotifyMessageMapper notifyMessageMapper;

    @Autowired
    private NewsService newsService;

    @Autowired
    private FeedbackService feedbackService;

    /**
     * 根据消息类型统计用户未读消息数
     *
     * @param memberId
     * @param messageType
     * @return
     */
    @Override
    public Integer countNotReadByMemberIdAndType(Integer memberId, Integer messageType) {
        List<News> newList = newsService.listAllNews();
        List<Integer> newIdList = newList.stream().map(News::getId).collect(Collectors.toList());
        List<NotifyMessage> newsMessageReadList = listByMemberIdAndMessageTypeAndTargetIdListAndReadSatus(memberId, NotifyMessageEnums.NEW_TYPE.getCode(), newIdList, NotifyMessageEnums.IS_READ);
        Integer count = newIdList.size() - newsMessageReadList.size();

        List<Feedback> allFeedback = feedbackService.listNotReadReplyByMemberId(memberId);
        Integer feedbackReplayNotReadCount = allFeedback.size();

        switch (messageType){
            case 0:
                return (count > 0 ? count : 0) + (feedbackReplayNotReadCount > 0 ? feedbackReplayNotReadCount : 0);
            case 1:
                return feedbackReplayNotReadCount > 0 ? feedbackReplayNotReadCount : 0;
            case 2:
                return count > 0 ? count : 0;
            default:
                return 0;
        }
    }

    private List<NotifyMessage> listByMemberIdAndMessageTypeAndTargetIdListAndReadSatus(Integer memberId, Integer messageType, List<Integer> targetIdList, NotifyMessageEnums readStatus){
        return list(new LambdaQueryWrapper<NotifyMessage>()
                .eq(NotifyMessage::getMemberId, memberId)
                .eq(NotifyMessage::getMessageType, messageType)
                .eq(NotifyMessage::getIsRead, readStatus.getCode())
                .in(!ObjectUtils.isEmpty(targetIdList), NotifyMessage::getTargetId, targetIdList));
    }

    /**
     * 当前用户某种消息全部已读
     *
     * @param memberId
     * @param messageType
     * @return
     */
    @Override
    public boolean readAllByMemberIdAndType(Integer memberId, Integer messageType) {
        if(messageType == 0){
            return notifyMessageMapper.readAllByMemberId(memberId) > 0;
        }else{
            return notifyMessageMapper.readAllByMemberIdAndType(memberId, messageType) > 0;
        }
    }

    /**
     * 具体某个用户的某条消息已读
     *
     * @param memberId
     * @param messageType
     * @param targetId
     * @return
     */
    @Override
    public boolean readByMemberAndTypeAndTarget(Integer memberId, Integer messageType, Integer targetId) {
        return notifyMessageMapper.readByMemberAndTypeAndTarget(memberId, messageType, targetId) > 0;
    }
}
